//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.common.cas;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(
    tags = {"【SF-BOOT】CAS前端控制器"}
)
@RestController
@RequestMapping({"/cas"})
public class CasController {
    private static final Logger log = LoggerFactory.getLogger(CasController.class);
    @Autowired
    private CasConfiguration casConfig;



    public CasController() {
    }

    @ApiOperation(
        value = "获取cas-client-url",
        notes = "获取cas-client-url"
    )
    @GetMapping({"/casClientUrl"})
    public R<String> getCasClientUrl() {
        return R.ok(this.casConfig.getCasClientUrl());
    }

    @ApiOperation(
        value = "获取cas-server-url",
        notes = "获取cas-server-url"
    )
    @GetMapping({"/casServerUrl"})
    public R<String> getCasServerDomain() {
        return R.ok(this.casConfig.getCasServerUrl());
    }

    @ApiOperation(
        value = "获取cas服务器登出地址",
        notes = "获取cas服务器登出地址"
    )
    @GetMapping({"/casServerLogoutUrl"})
    public R<String> getCasServerLogoutUrl() {
        return R.ok(this.casConfig.getCasServerLogoutUrl());
    }

    @ApiOperation(
        value = "cas登出",
        notes = "cas登出"
    )
    @GetMapping({"/logout"})
    public R<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session != null) {
            session.removeAttribute("_const_cas_assertion_");
            session.invalidate();
            log.info("cas logout, removed session!");
        }

        String callbackUrl = DefaultAjaxAuthenticationRedirectStrategy.getCallbackUrl(request, this.casConfig.getHomePage());
        return R.ok(callbackUrl);
    }

    @ApiOperation(
        value = "cas登出",
        notes = "cas登出，后台直接跳转到登录"
    )
    @GetMapping({"/logout2"})
    public void logout2(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        HttpSession session = request.getSession();
        if (session != null) {
            session.removeAttribute("_const_cas_assertion_");
            session.invalidate();
            log.info("cas logout2, removed session!");
        }

        String callbackUrl = DefaultAjaxAuthenticationRedirectStrategy.getCallbackUrl(request, this.casConfig.getHomePage());
        String redirectUrl = this.casConfig.getCasServerLogoutUrl() + "?service=" + URLEncoder.encode(callbackUrl, "UTF-8");
        response.setStatus(302);
        response.setHeader("location", redirectUrl);
    }

    @ApiOperation(
        value = "cas登录后回调地址",
        notes = "框架自动回调，应用不需要调用此接口。如果不满足，可以通过配置来自定义"
    )
    @GetMapping({"/login"})
    public void login(@RequestParam(value = "clientPage",required = false) String clientPage, HttpServletResponse response) {
        response.setStatus(302);
        clientPage = StringUtils.isEmpty(clientPage) ? this.casConfig.getHomePage() : clientPage;
        response.setHeader("location", clientPage);
    }
}
